package com.sanjishu.enterlab.electricity.sale.service;

import com.alibaba.fastjson.JSON;
import com.sanjishu.enterlab.electricity.sale.domain.responseModel.ResponseCompany;
import com.sanjishu.enterlab.electricity.sale.utils.HttpUtils;
import com.thoughtworks.xstream.core.util.Base64Encoder;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Encoder;

import java.util.HashMap;
import java.util.Map;

@Service
public class BLDistinguishService {
    private static final Logger log = LoggerFactory.getLogger(FileService.class);

    @Autowired
    private RestTemplate restTemplate;

    @Value("${business.license.key}")
    private String licenseKey;
    @Value("${business.license.secret}")
    private String licenseSecret;
    @Value("${business.license.code}")
    private String licenseCode;
    @Value("${business.license.path}")
    private String licensePath;

    public ResponseCompany getMessage(MultipartFile file) throws Exception {
        BASE64Encoder encoder = new BASE64Encoder();
        String imgCode = encoder.encode(file.getBytes());
        String bodys = "{\"image\":\""+ imgCode +"\"}";
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        headers.set("Authorization", "APPCODE " + licenseCode);
        HttpEntity requestEntity = new HttpEntity(bodys, headers);
        ResponseEntity<String> response = restTemplate.exchange(licensePath, HttpMethod.POST, requestEntity, String.class);
        log.info("url:{} data:{} response:{}", licensePath, bodys, response);
        System.out.print(response.getBody());

        ResponseCompany responseCompany = JSON.parseObject(response.getBody(), ResponseCompany.class);
        return responseCompany;
    }
}
